package com.fit.spring.boot.proxy.extension.interceptor;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;

import com.fit.spring.boot.proxy.extension.utils.ReflectUtil;

/**
 * 方法拦截器配置
 * @author hcl
 *
 */
public abstract class MethodInterceptorConfigurationAware extends InterceptorConfigurationAware{
	
	/**
	 * 设置此注解后,只有当目标类或目标方法上有此注解时才会调用getBeanMethodInterceptorsKeys方法
	 */
	private Class<Annotation>[] targetAnnotations;
	
	
	@Override
	public String[] getBeanInterceptorsKeys(Config config) {
		return null;
	}
	
	/**
	 * 过滤非目标注解
	 */
	@Override
	public String[] getBeanMethodInterceptorsKeys(Config config) {
		Map<String,Annotation> annotations = new HashMap<String,Annotation>();
		if(targetAnnotations == null || targetAnnotations.length == 0){
			return getBeanMethodInterceptorsKeys(config, annotations);
		}
		for(Class<Annotation> ann : targetAnnotations){
			Annotation annObject = null;
			try {
				annObject = ReflectUtil.getMethodInterfacesAnnotation(config.getClass(), config.getMethod(), ann);
			} catch (NoSuchMethodException e) {
			}
			if(annObject != null){
				annotations.put(annObject.annotationType().getName(), annObject);
			}
		}
		if(!annotations.isEmpty()){
			return getBeanMethodInterceptorsKeys(config, annotations);
		}
		return null;
	}
	
	/**
	 * 获取bean方法拦截器key
	 * @param config
	 * @param annotations key=注解类全名,value=注解对象
	 * @return
	 */
	public abstract String[] getBeanMethodInterceptorsKeys(Config config, Map<String,Annotation> annotations);
	
	/**
	 * 设置注解,设置后只有当此class上或此Method上有此注解时才会调用getBeanMethodInterceptorsKeys方法
	 * @param methodAnnotations
	 */
	public void setTargeAnnotations(Class<Annotation>[] targetAnnotations) {
		this.targetAnnotations = targetAnnotations;
	}
	
}
